package cn.net.cfss.fgbp.provider.manage.biz;

import cn.net.cfss.fgbp.base.common.biz.BaseBiz;
import cn.net.cfss.fgbp.base.constant.ErrCodeEnum;
import cn.net.cfss.fgbp.base.except.CodeException;
import cn.net.cfss.fgbp.provider.manage.dao.FscHolidayDtoMapper;
import cn.net.cfss.fgbp.provider.manage.service.CacheService;
import cn.net.cfss.fgbp.provider.manage.utils.DateUtil;
import cn.net.cfss.fgbp.spi.manage.constant.DeleteFlagEnum;
import cn.net.cfss.fgbp.spi.manage.constant.HolidayTypeEnum;
import cn.net.cfss.fgbp.spi.manage.dto.FscHolidayDto;
import cn.net.cfss.fgbp.spi.manage.vo.FscHolidayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author wutao
 * @version 1.0
 * @date 2021/3/5 14:26
 */
@Slf4j
@Service
public class FscHolidayBiz extends BaseBiz<FscHolidayDtoMapper, FscHolidayDto> {

    @Autowired
    private CacheService cacheService;





    public List<FscHolidayDto> queryAllHoliday() {
        return mapper.queryAllHoliday();
    }




    public HolidayTypeEnum queryTypeByDateForCache(Date date) {
        if (date == null) {
            throw new CodeException(ErrCodeEnum.ERR, "日期字段未传");
        }

        String format = "yyyy-MM-dd";

        String formatDate = DateUtil.format(date, format);

        HolidayTypeEnum holidayTypeEnum = null;

        //缓存失效
        //查询数据库
        FscHolidayDto search = new FscHolidayDto();
        search.setDate(formatDate);
        search.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        FscHolidayDto fscHolidayDto = selectOne(search);
        holidayTypeEnum = fscHolidayDto != null ? fscHolidayDto.getType() : null;


        if (holidayTypeEnum != null) {
            return holidayTypeEnum;
        }

        //查询完缓存或者数据库后，没有查询到当天的法定节假日安排，则按照周一-周五上班，周六-周日休息进行计算
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        switch (weekday) {
            case 2 :
            case 3 :
            case 4 :
            case 5 :
            case 6 :
                holidayTypeEnum = HolidayTypeEnum.WORK;
                break;
            case 1 :
            case 7 :
                holidayTypeEnum = HolidayTypeEnum.HOLIDAY;
                break;
            default:
                holidayTypeEnum = HolidayTypeEnum.WORK;
                break;
        }
        return holidayTypeEnum;
    }

    public List<FscHolidayVo> queryPage(FscHolidayVo search) {
        return mapper.queryPage(search);
    }
}
